package com.ticket.service.impl;

import com.ticket.mapper.PlaneMapper;
import com.ticket.pojo.PageBean;
import com.ticket.pojo.Plane;
import com.ticket.service.PlaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PlaneServiceImpl implements PlaneService {
    @Autowired
    private PlaneMapper planeMapper;

    @Override
    public List<Plane> selectAll() {
        return null;
    }

    @Override
    public void add(Plane plane) {
        planeMapper.add(plane);
    }

    @Override
    public void update(Plane plane) {
        planeMapper.update(plane);
    }

    @Override
    public void deleteById(int id) {
        planeMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(int[] ids) {
        planeMapper.deleteByIds(ids);
    }

    @Override
    public PageBean<Plane> selectByPage(int currentPage, int pageSize) {
        List<Plane> planes = planeMapper.selectByPage((currentPage - 1) * pageSize, pageSize);
        int totalCount = planeMapper.selectTotalCount();
        System.out.println(planes);
        //封装
        PageBean<Plane> pageBean = new PageBean<>();
        pageBean.setRows(planes);
        pageBean.setTotalCount(totalCount);

        return pageBean;
    }

    @Override
    public PageBean<Plane> selectByPageAndCondition(int currentPage, int pageSize, Plane plane) {
        //起始索引begin, 每页数据个数size
        //处理plane条件，模糊表达式
        String company = plane.getCompany();
        if (company != null && company.length() != 0) {
            System.out.println(company.length());
            plane.setCompany("%" + company + "%");
        }

        String airlineCode = plane.getAirlineCode();
        if (airlineCode != null && airlineCode.length() != 0) {
            plane.setAirlineCode("%" + airlineCode + "%");
        }

        String mode = plane.getMode();
        if (mode != null && mode.length() != 0) {
            plane.setMode("%" + mode + "%");
        }
        String startCity = plane.getStartCity();
        String startDrome = plane.getStartDrome();
//        Date startTime = plane.getStartTime();
        String arriveCity = plane.getArriveCity();
        String arriveDrome = plane.getArriveDrome();
//        Date arriveTime = plane.getArriveTime();
        if (startCity != null && startCity.length() != 0) {
            plane.setStartCity("%" + startCity + "%");
        }
        if (arriveCity != null && arriveCity.length() != 0) {
            plane.setArriveCity("%" + arriveCity + "%");
        }
        if (startDrome != null && startDrome.length() != 0) {
            plane.setStartDrome("%" + startDrome + "%");
        }
        if (arriveDrome != null && arriveDrome.length() != 0) {
            plane.setArriveDrome("%" + arriveDrome + "%");
        }
//        if (arriveTime != null) {
//            plane.setArriveTime("%" + arriveTime + "%");
//        }
//        if (startTime != null) {
//            plane.setStartTime("%" + startTime + "%");
//        }

        List<Plane> planes = planeMapper.selectByPageAndCondition((currentPage - 1) * pageSize, pageSize, plane);
        int totalCount = planeMapper.selectTotalCountByCondition(plane);

        System.out.println(planes);
        System.out.println(totalCount);
        //5.封装
        PageBean<Plane> pageBean = new PageBean<>();
        pageBean.setRows(planes);
        pageBean.setTotalCount(totalCount);

        return pageBean;
    }
}
